//
//  ZDWaitPayController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/19.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDWaitPayController.h"
#import "ZDOrderModel.h"
#import "NSDictionary+ToArray.h"
#import "ZDOrderFirstCell.h"
#import "ZDOrderLastCell.h"
#import "ZDOrderGoodsCell.h"
#import "ZDOldManModel.h"
#import "MakerTools.h"

static NSString *const ZDOrderFirstCellId = @"ZDOrderFirstCell";
static NSString *const ZDOrderLastCellId = @"ZDOrderLastCell";
static NSString *const ZDOrderGoodsCellId = @"ZDOrderGoodsCell";

@interface ZDWaitPayController ()

@end

@implementation ZDWaitPayController

- (void)setupUI
{
    [super setupUI];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDOrderFirstCell" bundle:nil] forCellReuseIdentifier:ZDOrderFirstCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDOrderLastCell" bundle:nil] forCellReuseIdentifier:ZDOrderLastCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDOrderGoodsCell" bundle:nil] forCellReuseIdentifier:ZDOrderGoodsCellId];
    [self resetTablewFrame:CGRectMake(0, 0, LEScreenWidth, LEScreenHeight -104)];
    [self initHeader];
    [self initFooter];
}
- (void)loadData
{
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"2" forKey:@"flag"];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"5" forKey:@"page"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:OrderCenterListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        
        if ([datas[@"order_list"] isKindOfClass:[NSDictionary class]])
        {
            [weakSelf hiddenNoDataView];
            NSMutableArray *tempt = [@{} toArray:datas[@"order_list"]];
            NSArray *tempModel = [ZDOrderModel mj_objectArrayWithKeyValuesArray:tempt];
            
            if (weakSelf.page == 1) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:tempModel];
                
            }else{
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }
            
        }else
        {
            [weakSelf addNoDataViewWithTipText:@"没有订单信息"];
            [weakSelf.dataSource removeAllObjects];
        }

        [weakSelf.tableView reloadData];
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

/**
 请求取消订单

 @param model <#model description#>
 */
- (void)requestCancelOrder:(ZDOrderModel *)model
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.order_id forKey:@"order_id"];
    
    [SVProgressHUD showWithStatus:@"正在取消中"];
     __weak typeof (self) weakSelf = self;
    [API requestPostAppendKey:CancelOrderUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:@"取消订单成功"];
            [weakSelf requestRefreshData];
        }

    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
    
}
- (void)requestPay:(ZDOrderModel *)orderModel
{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    ZDOldManModel *oldMan = [MakerTools readFromSandBox:OLDMAN_INFO];
    NSString *cardId = oldMan.idcard;
    NSString *UDID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    
    [dict setObject:cardId forKey:@"idcard"];
    [dict setObject:orderModel.pay_sn forKey:@"pay_sn"];
    [dict setObject:UDID forKey:@"device"];
    
    [SVProgressHUD showWithStatus:@"正在支付中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:WaitPayGoPayUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:@"支付成功"];
            [weakSelf requestRefreshData];
        }
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

    
}
#pragma mark- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ZDOrderModel *orderModel = self.dataSource[section];
    return orderModel.extend_order_goods.count + 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    ZDOrderModel *orderModel = self.dataSource[indexPath.section];
    
    if (indexPath.row == 0) {
        ZDOrderFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDOrderFirstCellId forIndexPath:indexPath];
        cell.model = orderModel;
        return cell;
    }
    if (indexPath.row == orderModel.extend_order_goods.count +1) {
        ZDOrderLastCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDOrderLastCellId forIndexPath:indexPath];
        cell.model = orderModel;
        cell.ordemode = WaitPay;
         __weak typeof (self) weakSelf = self;
        [cell orderCellClick:^(NSInteger operationTpye, ZDOrderModel *orderModel) {
            if (operationTpye == 1) {
                //取消订单
                [weakSelf handelCancel:orderModel];
            }else if (operationTpye == 2){
                //支付
                [weakSelf requestPay:orderModel];
            }
            
        }];
        return cell;
    }else{
        ZDOrderGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDOrderGoodsCellId forIndexPath:indexPath];
        cell.goodsDetailModel = orderModel.extend_order_goods[indexPath.row-1];
        return cell;
    }
}
#pragma mark- 点击事件
- (void)handelCancel:(ZDOrderModel *)orderModel
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"要取消订单吗" preferredStyle:UIAlertControllerStyleAlert];
    __weak typeof (self) weakSelf = self;
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //发送删除请求
        ZDLog(@"%s---%d---删除操作哈",__func__,__LINE__);
        [weakSelf requestCancelOrder:orderModel];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"算了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:sureAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

}
@end






